package tsinghua.smxq.mc;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends Activity {
	EditText username, userpwd, userpwdassure;
	Button regsubmit, regcancel;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_view);

		username = (EditText) findViewById(R.id.et_uname);
		userpwd = (EditText) findViewById(R.id.et_upwd);
		userpwdassure = (EditText) findViewById(R.id.et_upwda);
		regsubmit = (Button) findViewById(R.id.reg_btn);
		regcancel = (Button) findViewById(R.id.reg_cancel_btn);

		regsubmit.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String strUname = username.getText().toString();
				if (strUname == "") {
					Toast.makeText(RegisterActivity.this, "请填写用户名",
							Toast.LENGTH_LONG).show();
					return;
				}
				String strUpwd = userpwd.getText().toString();
				if (strUpwd == "") {
					Toast.makeText(RegisterActivity.this, "请填写密码",
							Toast.LENGTH_LONG).show();
					return;
				}
				String strUpwda = userpwdassure.getText().toString();
				if (strUpwda == "") {
					Toast.makeText(RegisterActivity.this, "请填写确认密码",
							Toast.LENGTH_LONG).show();
					return;
				} else if (strUpwda.compareTo(strUpwd) != 0) {
					Toast.makeText(RegisterActivity.this, "密码与确认密码不同",
							Toast.LENGTH_LONG).show();
					return;
				}
				HttpClient httpclient = new DefaultHttpClient();
				ArrayList<NameValuePair> qparams = new ArrayList<NameValuePair>();
				qparams.add(new BasicNameValuePair("method", "register"));
				qparams.add(new BasicNameValuePair("username", strUname));
				qparams.add(new BasicNameValuePair("password", strUpwd));
				URI uri;
				try {
					uri = URIUtils.createURI("http", Constants.serverIP, 8080,
							"MobileCityServer/UserServlet",
							URLEncodedUtils.format(qparams, "UTF-8"), null);
					HttpGet httpget = new HttpGet(uri);
					HttpResponse response = httpclient.execute(httpget);
					HttpEntity entity = response.getEntity();
					if (entity != null) {
						String contentString = EntityUtils.toString(entity);
						JSONObject o = new JSONObject(contentString);
						if (o.getInt("status") == 1) {
							RegisterActivity.this.startActivity(new Intent(
									RegisterActivity.this, FaceActivity.class));
						}
					}
				} catch (URISyntaxException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		});

		regcancel.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RegisterActivity.this.finish();
			}

		});

	}

}
